#!/usr/bin/env ruby

require File.dirname(__FILE__) + "/../config/environment"
require "generator"

addresses = User.count(
  :conditions => {
    :status => %w(suspended deleted),
    :creation_time => Time.now - 28.days..Time.now
  },
  :group => :creation_ip
)

addresses.each do |address, count|
  next unless count > 1
  next if Acl.where(:address => address).exists?

  Acl.create({
               :address => address,
               :k => "no_account_creation",
               :v => "auto_spam_block"
             }, { :without_protection => true })

  puts "Blocked #{address}"
end

acls = Acl.find(:all, :conditions => {
                  :k => "no_account_creation",
                  :v => "auto_spam_block"
                })

acls.each do |acl|
  next if addresses[acl.address]

  acl.delete

  puts "Unblocked #{acl.address}"
end

exit 0
